<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserInsertRequest;
use App\User;
use Hash;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //

        $keywords = $request->input('keywords');
        //如果参数为空s
        // if(empty($keywords)) {
        //     $users = User::orderBy('id','desc')
        //         ->paginate($request->input('num',10));
        // }else{//如果不为空
        //     $users = User::orderBy('id','desc')
        //     ->where('username','like','%'.$keywords.'%')
        //     ->paginate($request->input('num',10));
        // }

        $users = User::orderBy('id','desc')
            ->where(function($query)use($keywords){
                //如果存在关键字参数
                if(!empty($keywords)) {
                    $query->where('username','like','%'.$keywords.'%');
                }
            })
            ->paginate($request->input('num',10));
       

        return view('admin.user.index',['users'=>$users,'data'=>$request->all()]);
    }

    /**
     * 用户添加
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserInsertRequest $request)
    {
        //表单验证  unique唯一
            // $this->validate($request, [
            //     'username' => 'required|regex:/^\w{6,20}$/|unique:users,username',
            //     'password' => 'required|regex:/^\S{6,20}$/|same:repassword',
            //     'email' =>'required|email'
            // ],[
            //     'username.required'=>'对不起用户名必填',
            //     'username.regex'=>'用户名格式不正确',
            //     'username.unique'=>'用户名已经存在',
            //     'password.required' => '密码必填',
            //     'password.regex' => '密码格式不正确',
            //     'password.same' => '两次密码不一致',
            //     'email.email'=>'邮箱格式不正确'
            // ]);//  /^\w+@\w+\.(com|cn|com.cn.....)$/

        //获取数据 入库
        $user = new User;
        $user -> username = $request->username;//
        $user -> password = Hash::make($request->input('password'));
        $user -> email = $request -> email;  
        $user -> age = 20;

        //图片上传
        if($request->hasFile('img')) {
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('img')->getClientOriginalExtension();
            //文件夹
            $dirname = './uploads/';
            //文件名
            $file = $filename .'.'. $suffix;
            //移动
            $request->file('img')->move($dirname,$file);
            //修改图片属性
            $user->img = $dirname.$file;
        }

        //写入
        $user->save();
        // if($user->save()) {
        //     return redirect('/user');
        // }else{
        //     return back();
        // }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
